﻿using HrEFCore;
using HrService;
using Microsoft.AspNetCore.Mvc;

namespace HrUI.Controllers
{
	public class CPCController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly CPCService cPCService;
		public CPCController(MyDbContext myDbContext, CPCService cPCService)
		{
			this.myDbContext = myDbContext;
			this.cPCService = cPCService;
		}
		public async Task<IActionResult> Index()
		{
			//查询所有
			List<CPC> list = await cPCService.CPCAllAsync();
			ViewData.Model = list;
			return View();
		}
		public async Task<IActionResult> Delete(short id)
		{
			//删除
			int row = await cPCService.DeleteCPCAsync(id);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("Index");
			}
		}
		public IActionResult Add()
		{
			//跳页面
			return View();
		}
		//添加
		public async Task<IActionResult> Add2(CPC cPC)
		{
			int result = await cPCService.AddCPCAsync(cPC);
			if (result > 0)
			{
				Content("<script>('添加成功')</script>");
				return RedirectToAction("Index");
			}
			else
			{
				Content("<script>('添加失败')</script>");
				return View();
			}
		}
        public async Task<IActionResult> JobTitle()
        {
            //跳转职称设置页面
            List<CPC> jobTitleList = await cPCService.InquireJobTitle();
            ViewData.Model = jobTitleList;
            return View();
        }
        public async Task<IActionResult> DeleteJobTile(string id)
        {
            //职称设置删除
            short mfkId;
            if (!short.TryParse(id, out mfkId))
            {
                // ID格式不正确，返回职位分类列表，并提示用户
                return RedirectToAction("JobTitle");
            }
            int row = await cPCService.DeleteCPCAsync(mfkId);
            if (row > 0)
            {
                return RedirectToAction("JobTitle");
            }
            else
            {
                return RedirectToAction("JobTitle");
            }
        }

        public async Task<IActionResult> Pay()
		{
            //跳转薪酬设置页面
            List<CPC> payList = await cPCService.InquirePay();
            ViewData.Model = payList;
            return View();
        }
		public async Task<IActionResult> CayAdd(CPC cPC)
		{
            //跳转薪酬设置添加
            return View();
        }
        public async Task<IActionResult> CayAdd2(CPC cPC)
        {
            //薪酬设置添加
            int result = await cPCService.AddPay(cPC);
            if (result > 0)
            {
                return RedirectToAction("Pay");
            }
            else
            {
                return View();
            }
        }
        public async Task<IActionResult> DeleteCay(string id)
        {
            //薪酬设置删除
            short mfkId;
            if (!short.TryParse(id, out mfkId))
            {
                // ID格式不正确，返回职位分类列表，并提示用户
                return RedirectToAction("Pay");
            }
            int row = await cPCService.DeleteCPCAsync(mfkId);
            if (row > 0)
            {
                return RedirectToAction("Pay");
            }
            else
            {
                return RedirectToAction("Pay");
            }
        }
    }
}
